package com.onec.service.netty.handler;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.embedded.EmbeddedChannel;

public class InHandlerDemo extends ChannelInboundHandlerAdapter {

  @Override
  public void handlerAdded(ChannelHandlerContext channelHandlerContext) throws Exception {
    super.handlerAdded(channelHandlerContext);
  }

  @Override
  public void channelRegistered(ChannelHandlerContext context) throws Exception {
    super.channelRegistered(context);
  }


}

class Test {

  @org.junit.Test
  public void test() {
    final InHandlerDemo demo = new InHandlerDemo();
    ChannelInitializer channelInitializer = new ChannelInitializer<EmbeddedChannel>() {
      @Override
      protected void initChannel(EmbeddedChannel ch) throws Exception {
        ch.pipeline().addLast(demo);
      }
    };
    EmbeddedChannel embeddedChannel = new EmbeddedChannel(channelInitializer);

    ByteBuf byteBuf = Unpooled.buffer();
    byteBuf.writeInt(1);

    embeddedChannel.writeInbound(byteBuf);
    embeddedChannel.flush();

    embeddedChannel.close();
  }
}
